package com.xhsj.user.pms.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.system.mapper.TbGlobalUserMapper;
import com.xhsj.user.pms.base.BaseController;
import com.xhsj.user.pms.common.constant.IdentifyConstant;
import com.xhsj.user.pms.common.constant.JumpConstant;
import com.xhsj.user.pms.common.constant.StatusConstant;
import com.xhsj.user.pms.overseas.entity.TbPamOverseasPrompt;
import com.xhsj.user.pms.patent.domain.ExamineModel;
import com.xhsj.user.pms.patent.domain.TransferModel;
import com.xhsj.user.pms.patent.mapper.*;
import com.xhsj.user.pms.workflow.dto.ExecProgressDto;
import com.xhsj.user.pms.workflow.mapper.WorkflowIdentitylinkMapper;
import com.xhsj.user.pms.workflow.mapper.WorkflowLinkMapper;
import com.xhsj.user.pms.workflow.model.WorkFlowScheduleModel;
import com.xhsj.user.pms.workflow.service.WorkflowExecService;
import com.xhsj.user.properties.RoleKeyNameProperites;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Auther: 罗季嘉
 * @Date: 2020/1/17 14:44
 */
@Slf4j
@Controller
@Api(value = "流程运行", description = "流程运行")
@RequestMapping("/workFlow")
@CrossOrigin(origins = "*", maxAge = 3600)
public class WorkFlowController extends BaseController {

    @Autowired
    private WorkflowExecService workflowExecService;

    @Autowired
    private TbGlobalUserMapper userMapper;

    @Autowired
    private JudgesUserMapper judgesUserMapper;

    @Autowired
    private TbPamInventorProposalIprMapper tbPamInventorProposalIprMapper;

    @Autowired
    private RoleKeyNameProperites roleKeyNameProperites;

    @Autowired
    private WorkflowLinkMapper workflowLinkMapper;

    @Autowired
    private DisclosureMapper disclosureMapper;

    @Autowired
    private DisclosureInventorMapper disclosureInventorMapper;


//    //技术负责人
    @Value("${rolekeyname.generalManager}")
    private String generalManager;
    //部门领导
    @Value("${dept.deptName}")
    private String deptName;
    //助理
    @Value("${rolekeyname.assistantName}")
    private String assistantName;


    /**
     * 查询用户审核列表
     *
     * @author 苏小林
     * @date 2020/2/24 15:08
     * @param: param
     */
    @ApiOperation(value = "查询用户审核列表", notes = "查询用户审核列表")
    @PostMapping("/findPatentAuditList")
    @ResponseBody
    public Message findPatentAuditList(@RequestBody Map<String, Object> param) {
        try {
        if (Objects.isNull(param.get("userId"))) {
            return Message.error(1, "当前用户id不能为空");
        }
        if (Objects.isNull(param.get("type"))) {
            return Message.error(1, "type不能为空");
        }
        IPage<ExamineModel> selectPage = workflowExecService.findPatentAuditList(param);
        return Message.success("成功", selectPage.getRecords(), selectPage.getTotal());

        } catch (Exception e) {
            log.error("查询用户审核列表异常信息，{}", e);
            e.printStackTrace();
            return Message.error(1, "查询用户审核列表失败");
        }
    }

    /**
     * 主管审核保存 - 最新优化
     * @author 苏小林
     * @date 2021/1/11 10:52
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "主管审核保存", notes = "主管审核保存")
    @PostMapping("/managerWorkFlowProgress/v1")
    @ResponseBody
    public Message managerWorkFlowProgress(@RequestBody ExecProgressDto param) {
        try {
            log.info("主管审核保存参数{}，", param);
            if (param.getFlowCode() == 0) {
                List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
                if (adviceIpr == null || adviceIpr.size() == 0) {
                    return Message.error(1, "请在提交交底书的时候指定IPR");
                }
                param.setUsers(adviceIpr);
                param.setLabel(IdentifyConstant.USER);
                param.setJumpForm(JumpConstant.IPR_CX_AUDIT);
            } else if (param.getFlowCode() == 1) {
                if(StringUtils.isBlank(param.getCreateUser()))
                    throw new Exception("交底书创建人不存在，不能继续下一步");
                String fullNameByUserId = userMapper.getFullNameByUserId(String.valueOf(param.getCreateUser()));
                if(StringUtils.isBlank(fullNameByUserId))
                    throw new Exception("交底书创建人已离职，不能继续下一步");
                param.setUsers(Arrays.asList(param.getCreateUser()));
                param.setLabel(IdentifyConstant.USER);
                param.setJumpForm(JumpConstant.CREATEBY_RESUBMIT_AUDIT);
            } else {
                return Message.error(1, "flowCode参数错误 原因只有0，1");
            }
            workflowExecService.managerWorkFlowProgress(param);
            return Message.success("主管审核成功");

        } catch (Exception e) {
            log.error("主管审核异常信息，{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * IPR查新沟通 - 最新优化
     * @author 苏小林
     * @date 2021/1/11 10:52
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "IPR查新沟通", notes = "IPR查新沟通")
    @PostMapping("/iprCommunicate")
    @ResponseBody
    public Message iprCommunicate(@RequestBody ExecProgressDto param) {
        log.info("IPR查新沟通参数{}，", param);
        try {

            int flowCode = param.getFlowCode();
            switch (flowCode) {
                // 通过
                case 0:
                    List<Long> judgesUserId = judgesUserMapper.getJudgesUserId(param.getDisclosureId());
                    if (judgesUserId == null || judgesUserId.size() == 0) {
                        return Message.error(1, "下一节点评审会成员不存在,请先完善评审会成员!");
                    }

                    // 查询指定IPR
                    List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
                    judgesUserId.addAll(adviceIpr);
                    Set<Long> longs = new HashSet<>(judgesUserId);
                    List<Long> judgesUserIds = new ArrayList<>(longs);
                    param.setUsers(judgesUserIds);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.JUDGES_FIRST_TRIAL_AUDIT);
                    workflowExecService.iprCommunicate(param);
                    break;
                // 闭案
                case 3:
                    param.setUsers(null);
                    param.setLabel(IdentifyConstant.USER);
                    workflowExecService.iprCommunicate(param);
                    break;
                // 沟通
                case 2:
                    if(StringUtils.isBlank(param.getCreateUser()))
                        throw new Exception("交底书创建人不存在，不能继续下一步");
                    String fullNameByUserId = userMapper.getFullNameByUserId(String.valueOf(param.getCreateUser()));
                    if(StringUtils.isBlank(fullNameByUserId))
                        throw new Exception("交底书创建人已离职，不能继续下一步");
                    param.setUsers(Arrays.asList(param.getCreateUser()));
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.CREATEBY_REPLAYIPR_AUDIT);
                    workflowExecService.iprCommunicate(param);
                    break;
                default:
                    return Message.error(1, "flowCode参数错误 原因只有0，1，2");
            }

            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("IPR查新沟通保存出错，{}", e);
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * 创建人回复IPR - 最新优化
     * @author 苏小林
     * @date 2021/1/11 10:54
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "创建人回复IPR", notes = "创建人回复IPR")
    @PostMapping("/creatorReplyIpr")
    @ResponseBody
    public Message creatorReplyIpr(@RequestBody ExecProgressDto param) {
        try {
            log.info("创建者与多个ipr查新沟通通过/退回{}，", param);
            if (param.getFlowCode() == 0) { // 沟通
                if(param.getUsers()  == null || param.getUsers().size() == 0)
                    throw new Exception("IPR不存在，不能继续下一步");
                List<String> users = param.getUsers().stream().map(user -> String.valueOf(user)).collect(Collectors.toList());
                Set<String> fullNameByUserIds = userMapper.getFullNameByUserIds(users);
                if(fullNameByUserIds == null || fullNameByUserIds.size() == 0 )
                    throw new Exception("IPR已离职，不能继续下一步");
                param.setUsers(param.getUsers());
                param.setLabel(IdentifyConstant.USER);
                param.setJumpForm(JumpConstant.IPR_CX_AUDIT);

            } else if (param.getFlowCode() == 3) { // 闭案
                param.setUsers(null);
                param.setLabel(IdentifyConstant.USER);
            } else {
                return Message.error(1, "flowCode参数错误 原因只有0，1");
            }

            workflowExecService.creatorReplyIpr(param);
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("创建者与多个ipr查新沟通通过/退回，{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * 保存反馈数据 - 最新优化
     * @author 苏小林
     * @date 2021/1/11 16:13
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "保存反馈数据", notes = "保存反馈数据")
    @PostMapping("/saveFeedback")
    @ResponseBody
    public Message saveFeedback(@RequestBody Map<String, Object> param) {
        try {
            log.info("保存反馈数据参数{}，", param);

            String nodeId = StringUtils.getStringValue(param, "nodeId");
            if ("7524DFC3666B40C5A6E6EEFD063F06EA".equals(nodeId)) {// 终审
                param.put("type",1);
            }else if("736ca4d2a36e475daefd92e9828a1489".equals(nodeId)){
                param.put("type",2);
            }else{
                param.put("type",0);
            }

            workflowExecService.saveJudgesReviewRecord(param);
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("创建者与多个ipr查新沟通通过/退回，{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * 评审会通过 (包含初审和终审) - 最新优化
     * @author 苏小林
     * @date 2021/1/11 17:32
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "评审会通过", notes = "评审会通过")
    @PostMapping("/firstTrialPass")
    @ResponseBody
    public Message firstTrialPass(@RequestBody ExecProgressDto param) {
        try {
            log.info("初审通过参数{}，", param);
            int flowCode = param.getFlowCode();
            switch (flowCode) {
                // 通过 -- 总经理确认
                case 0:
                    workflowExecService.firstTrialPass(param);
                    break;
                case 1:
                    //获取建议ipr
                    List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
                    if (adviceIpr == null || adviceIpr.size() == 0) {
                        return Message.error(1, "IPR不存在");
                    }
                    param.setUsers(adviceIpr);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.IPR_CX_AUDIT);
                    if ("7524DFC3666B40C5A6E6EEFD063F06EA".equals(param.getNodeId())) {// 终审
                        param.setJumpForm(JumpConstant.IPR_BX_AUDIT);
                    }
                    workflowExecService.firstTrialRe(param);
                    break;
                default:
                    return Message.error(1, "flowCode参数错误 原因只有0，1");
            }
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }

    }


        /**
     * 评审会确认 (包含初审和终审) - 最新优化
     * @author 苏小林
     * @date 2021/1/12 13:22
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "评审会确认", notes = "评审会确认")
    @PostMapping("/judgesConfirm")
    @ResponseBody
    public Message judgesConfirm(@RequestBody ExecProgressDto param) {
        try {
            log.info("评审会确认参数{}，", param);
            int flowCode = param.getFlowCode();
            switch (flowCode) {
                // 通过 -- 总经理确认
                case 0:
                    String generalManager = roleKeyNameProperites.getGeneralManager();
                    Long roleId = userMapper.getRoleIdByRoleKeyName(generalManager);
                    if(roleId == null)
                        throw new Exception("总经理角色不存在");
                    List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(generalManager);
                    if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                        throw new Exception("总经理该角色下无用户");
                    param.setUsers(Arrays.asList(roleId));
                    param.setLabel(IdentifyConstant.ROLE);
                    param.setJumpForm(JumpConstant.FIRST_TRIAL_ZJL_CONFIRM_AUDIT);
                    if ("04198012fc1c49b68959c05bd492d80f".equals(param.getNodeId())) { // 终审确认节点
                        param.setJumpForm(JumpConstant.FINAL_TRIAL_ZJL_CONFIRM_AUDIT);
                    }

                    workflowExecService.execProgress2(param);
                    break;
                // 退回 -- ipr重新查新 || IPR编写权利要求文件
                case 1:
                    // 建议IPR
                    List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
                    if (adviceIpr == null || adviceIpr.size() == 0) {
                        return Message.error(1, "IPR不存在");
                    }
                    param.setUsers(adviceIpr);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.IPR_CX_AUDIT);
                    if ("04198012fc1c49b68959c05bd492d80f".equals(param.getNodeId())) { // 终审确认节点
                        param.setJumpForm(JumpConstant.IPR_BX_AUDIT);
                    }
                    workflowExecService.execProgress2(param);
                    break;
                // 闭案
                case 3:
                    param.setUsers(null);
                    param.setLabel(IdentifyConstant.USER);
                    workflowExecService.execProgress2(param);
                    break;
                default:
                    return Message.error(1, "flowCode参数错误 原因只有0，1，2");
            }
            disclosureMapper.updateSignificanceIdentify(param.getDisclosureId(),param.getSignificanceIdentify());
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * 评审会总经理确认 (包含初审和终审) - 最新优化
     * @author 苏小林
     * @date 2021/1/12 13:22
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "评审会总经理确认", notes = "评审会总经理确认")
    @PostMapping("/judgesGeneralManagerConfirm")
    @ResponseBody
    public Message judgesGeneralManagerConfirm(@RequestBody ExecProgressDto param) {
        try {
            log.info("评审会总经理确认参数{}，", param);
            int flowCode = param.getFlowCode();
            // 建议IPR
            List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
            if (adviceIpr == null || adviceIpr.size() == 0) {
                return Message.error(1, "IPR不存在");
            }
            switch (flowCode) {
                // 通过 -- 权利要求文件 || 说明书
                case 0:
                    param.setUsers(adviceIpr);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.IPR_BX_AUDIT);
                    if ("77409ADFDC384F46AA3F6B415893E432".equals(param.getNodeId())) { // 终审总经理确认节点
                        param.setJumpForm(JumpConstant.IPR_EDIT_COMPLETE_FILE);
                    }
                    workflowExecService.execProgress2(param);
                    break;
                // 退回 -- ipr重新查新 || ipr重新编写权利要求文件
                case 1:
                    param.setUsers(adviceIpr);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.IPR_CX_AUDIT);
                    if ("77409ADFDC384F46AA3F6B415893E432".equals(param.getNodeId())) { // 终审总经理确认节点
                        param.setJumpForm(JumpConstant.IPR_BX_AUDIT);
                    }
                    workflowExecService.execProgress2(param);
                    break;
                // 闭案
                case 3:
                    param.setUsers(null);
                    param.setLabel(IdentifyConstant.USER);
                    workflowExecService.execProgress2(param);
                    break;
                default:
                    return Message.error(1, "flowCode参数错误 原因只有0，1，2");
            }
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * IPR编写权利要求文件 - 最新优化
     * @author 苏小林
     * @date 2021/1/12 15:08
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "IPR编写权利要求文件", notes = "IPR编写权利要求文件")
    @PostMapping("/iprEditFile")
    @ResponseBody
    public Message iprEditFile(@RequestBody ExecProgressDto param) {
        try {
            log.info("IPR编写权利要求文件参数{}，", param);
            List<Long> judgesUserId = judgesUserMapper.getJudgesUserId(param.getDisclosureId());
            if (judgesUserId == null || judgesUserId.size() == 0) {
                return Message.error(1, "下一节点评审会成员不存在,请先完善评审会成员!");
            }

            // 查询指定IPR
            List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
            judgesUserId.addAll(adviceIpr);
            param.setUsers(judgesUserId.stream().distinct().collect(Collectors.toList()));
            param.setLabel(IdentifyConstant.USER);
            param.setJumpForm(JumpConstant.JUDGES_FINAL_TRIAL_AUDIT);
            workflowExecService.iprEditFile(param);
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * IPR编写完整申请文件 - 最新优化
     * @author 苏小林
     * @date 2021/1/13 10:20
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "IPR编写完整申请文件", notes = "IPR编写完整申请文件")
    @PostMapping("/iprEditCompleteFile")
    @ResponseBody
    public Message iprEditCompleteFile(@RequestBody ExecProgressDto param) {
        try {
            log.info("IPR编写完整申请文件参数{}，", param);
            List<Long> users = param.getUsers();
//            if (users == null || users.size() == 0) {
//                return Message.error(1, "互审IPR不能为空");
//            }
            // 查询交底书相关评审会负责人
            List<Long> list_f = disclosureMapper.getPatentUserIdByDisclosureId(param.getDisclosureId());
            if (list_f == null || list_f.size() == 0) {
                throw new Exception("评审会负责人为空,请联系管理员完善评审会负责人!");
            }
            // 知识产权部负责人- 资深专利工程师1
            String zszl1Name = roleKeyNameProperites.getZszl1();
            Long roleId = userMapper.getRoleIdByRoleKeyName(zszl1Name);
            if(roleId == null)
                throw new Exception("知识产权部负责人- 资深专利工程师1角色不存在");
            List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(zszl1Name);
            if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                throw new Exception("知识产权部负责人- 资深专利工程师1角色下无用户");
            // 发明人（ 比权重 && 公司员工&&排除离职）
            List<Long> InventorUserId =  disclosureInventorMapper.getInventorUserId(param.getDisclosureId());
            if(InventorUserId.size() == 0)
                throw new Exception("无发明人或发明人无公司内部人员");
            users.addAll(InventorUserId);
            users.addAll(userIdByRoleName);
            users.addAll(list_f);
            param.setUsers(users.stream().distinct().collect(Collectors.toList()));
            param.setLabel(IdentifyConstant.USER);
            param.setJumpForm(JumpConstant.IPR_HS_AUDIT);
            workflowExecService.iprEditCompleteFile(param);
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * IPR互审 - 最新优化
     * @author 苏小林
     * @date 2021/1/13 10:43
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "IPR互审", notes = "IPR互审")
    @PostMapping("/iprMutualAudit")
    @ResponseBody
    public Message iprMutualAudit(@RequestBody ExecProgressDto param) {
        try {
            log.info("IPR互审参数{}，", param);

            // 查询指定IPR
            List<Long> adviceIpr = tbPamInventorProposalIprMapper.getAdviceIpr(param.getDisclosureId());
            if (adviceIpr == null || adviceIpr.size() == 0) {
                return Message.error(1, "交底书指定的IPR不存在");
            }
            int flowCode = param.getFlowCode();
            switch (flowCode) {
                // 通过 -- 互审总经理确认
                case 0:
                    String generalManager = roleKeyNameProperites.getGeneralManager();
                    Long roleId = userMapper.getRoleIdByRoleKeyName(generalManager);
                    if(roleId == null)
                        throw new Exception("总经理角色不存在");
                    List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(generalManager);
                    if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                        throw new Exception("总经理该角色下无用户");
                    param.setUsers(Arrays.asList(roleId));
                    param.setLabel(IdentifyConstant.ROLE);
                    param.setJumpForm(JumpConstant.HS_ZJL_CONFIRM_AUDIT);
                    workflowExecService.iprMutualAudit(param);
//                    workflowExecService.execProgress2(param);
                    break;
                // 退回 -- IPR重新编写 完整申请文件
                case 1:
                    param.setUsers(adviceIpr);
                    param.setLabel(IdentifyConstant.USER);
                    param.setJumpForm(JumpConstant.IPR_EDIT_COMPLETE_FILE);
                    workflowExecService.iprReMutualAudit(param,2);
                    break;
                default:
                    return Message.error(1, "flowCode参数错误 原因只有0，1");
            }

            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * 总经理确认互审 - 最新优化
     * @author 苏小林
     * @date 2021/1/13 14:08
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "总经理确认互审", notes = "总经理确认互审")
    @PostMapping("/generalManagerConfirmHs")
    @ResponseBody
    public Message generalManagerConfirmHs(@RequestBody ExecProgressDto param) {
        try {
            log.info("总经理确认互审参数{}，", param);
            if (param.getFlowCode() != 0) {
                return Message.error(1, "flowCode标识不正确");
            }
            // 获取流程专员角色
            String assistantName = roleKeyNameProperites.getAssistantName();
            Long roleId = userMapper.getRoleIdByRoleKeyName(assistantName);
            if(roleId == null)
                throw new Exception("流程专员角色不存在");
            List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(assistantName);
            if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                throw new Exception("流程专员该角色下无用户");
            param.setUsers(Arrays.asList(roleId));
            param.setLabel(IdentifyConstant.ROLE);
            param.setJumpForm(JumpConstant.PATENT_SUBMIT);
            workflowExecService.execProgress2(param);
            return Message.success("审核成功");
        } catch (Exception e) {
            log.error("失败{}", e);
            e.printStackTrace();
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * 递交申请文件 - 最新优化
     * @author 苏小林
     * @date 2021/1/13 14:07
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "递交申请文件", notes = "递交申请文件")
    @PostMapping("/submitApplicationFile")
    @ResponseBody
    public Message submitApplicationFile(@RequestBody ExecProgressDto param) {
        try {
            log.info("文件提交参数{}，", param);
            if (param.getFlowCode() == 0) {
                // 流程专员
                String assistantName = roleKeyNameProperites.getAssistantName();
                Long roleId = userMapper.getRoleIdByRoleKeyName(assistantName);
                if(roleId == null)
                    throw new Exception("流程专员角色不存在");
                List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(assistantName);
                if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                    throw new Exception("流程专员该角色下无用户");
                param.setUsers(Arrays.asList(roleId));
                param.setLabel(IdentifyConstant.ROLE);
                param.setJumpForm(JumpConstant.WAIT_FOR_NOTICE);
            } else {
                return Message.error(1, "flowCode参数错误 原因只有0");
            }
            workflowExecService.submitApplicationFile(param);
            return Message.success();
        } catch (Exception e) {
            log.error("递交申请文件出错{}",e);
            return Message.error(1, e.getMessage());
        }
    }



    /**
     * 通知提交 - 最新优化
     * @author 苏小林
     * @date 2021/1/13 14:57
     * @param: param
     * @return: com.xhsj.user.utils.Message
     */
    @ApiOperation(value = "通知提交", notes = "通知提交    ")
    @PostMapping("/noticeSubmit")
    @ResponseBody
    public Message noticeSubmit(@RequestBody Map<String, Object> param) {
        try {
            log.info("通知提交参数{}，", param);
            if (StringUtils.isNull(param.get("disclosureId"))) {
                return Message.error(1, "交底书id不能为空");
            }
            if (StringUtils.isNull(param.get("flowCode"))) {
                return Message.error(1, "流程线标识参数不能为空");
            }

            String assistantName = roleKeyNameProperites.getAssistantName();
            Long roleId = userMapper.getRoleIdByRoleKeyName(assistantName);
            if(roleId == null)
                throw new Exception("流程专员角色不存在");
            List<Long> userIdByRoleName = userMapper.getUserIdByRoleName(assistantName);
            if(userIdByRoleName == null || userIdByRoleName.size() == 0)
                throw new Exception("流程专员该角色下无用户");
            if ("0".equals(String.valueOf(param.get("flowCode")))) {  // 提交申请文件
                param.put("label", IdentifyConstant.ROLE);
                param.put("users", Arrays.asList(roleId));
                param.put("jumpForm", JumpConstant.APPLICATION_FEE_PAID);
            } else if ("4".equals(String.valueOf(param.get("flowCode")))) {  // 结案

            } else if ("5".equals(String.valueOf(param.get("flowCode")))) {  // 保存

            }else {
                return Message.error(1, "flowCode参数错误 原因只有0 1 ");
            }

            workflowExecService.noticeCommitWorkFlowProgress(param);
            return Message.success();
        } catch (Exception e) {
            log.error("通知提交保存出错,{}",e);
            return Message.error(1, e.getMessage());
        }
    }



    @ApiOperation(value = "进度查询", notes = "进度查询")
    @ApiImplicitParam(name = "param", value = "{\n" +
            "\"pageNum\":当前页,\n" +
            "\"pageSize\":有几条数据,\n" +
            "\"disclosureId\":\"交底书ID/专利ID\"\n" +
            "}", required = true, dataType = "Map")
    @PostMapping("/getWorkFlowSchedule/v1")
    @ResponseBody
    public Message getWorkFlowSchedule(@RequestBody Map<String, Object> param) {
        try {
            log.info("进度查询参数{}，", param);
            if (StringUtils.isNull(param.get("disclosureId"))) {
                return Message.error(1, "参数不能为空");
            }
            String disclosureId = String.valueOf(param.get("disclosureId"));
            int pageNum = 1;
            int pageSize = 20;
            if (StringUtils.isNotNull(param.get("pageNum"))) {
                pageNum = Integer.parseInt(String.valueOf(param.get("pageNum")));
            }
            if (StringUtils.isNotNull(param.get("pageSize"))) {
                pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
            }
            IPage<WorkFlowScheduleModel> disclosureProgress = workflowExecService.getWorkFlowSchedule(disclosureId,pageNum,pageSize);
            return Message.success("进度查询成功",disclosureProgress.getRecords(),disclosureProgress.getTotal(),disclosureProgress.getPages());
        }catch (Exception e){
            log.error("进度查询异常信息:{}",e);
            e.printStackTrace();
            return Message.error(1, "进度查询失败");
        }
    }

    //流程节点列表
    @ApiOperation(value = "流程节点列表", notes = "流程节点列表")
    @GetMapping("/getWorkFlowNodeList/v1")
    @ResponseBody
    public Message getWorkFlowNodeList(@RequestParam(value = "disclosureId") String disclosureId) {
        try {
            List<Map<String,Object>> disclosureProgress = workflowExecService.getWorkFlowNodeList(disclosureId);
            return Message.success("流程节点成功",disclosureProgress);
        } catch (Exception e) {
            log.error("流程节点信息:{}", e);
            return Message.error("流程节点失败");
        }
    }

    //流程节点列表
    @ApiOperation(value = "流程当前节点", notes = "流程节点列表")
    @GetMapping("/getCurrentNodeNum/v1")
    @ResponseBody
    public Message getCurrentNodeNum(@RequestParam(value = "disclosureId") String disclosureId) {
        try {
            Integer currentNodeId = workflowExecService.getCurrentNodeNum(disclosureId);
            return Message.success("流程当前节点成功",currentNodeId);
        } catch (Exception e) {
            log.error("流程当前节点异常信息:{}", e);
            return Message.error("流程节点失败");
        }
    }

    @ApiOperation(value = "转出列表", notes = "转出列表")
    @PostMapping("/findPatentAuditTransferOutList")
    @ResponseBody
    public Message findPatentAuditTransferOutList(@RequestBody Map<String, Object> param) {

        if (Objects.isNull(param.get("userId"))) {
            return Message.error(1, "当前用户id不能为空");
        }

        if (Objects.isNull(param.get("taskType"))) {
            return Message.error(1, "taskType任务类型");
        }
        IPage<TransferModel> list = workflowExecService.findPatentAuditTransferOutList(param);
        return Message.success("成功",list.getRecords(),list.getTotal(),list.getPages());


    }


    @ApiOperation(value = "转入列表", notes = "转入列表")
    @PostMapping("/findPatentAuditTransferList")
    @ResponseBody
    public Message findPatentAuditTransferList(@RequestBody Map<String, Object> param) {

        if (Objects.isNull(param.get("userId"))) {
            return Message.error(1, "当前用户id不能为空");
        }

        if (Objects.isNull(param.get("taskType"))) {
            return Message.error(1, "taskType任务类型");
        }

        IPage<TransferModel> list = workflowExecService.findPatentAuditTransferList(param);

        return Message.success("成功",list.getRecords(),list.getTotal(),list.getPages());

    }

    // 流程节点列表
    @ApiOperation(value = "流程列表", notes = "流程列表")
    @GetMapping("/getNodeList/v1")
    @ResponseBody
    public Message getNodeList() {
        try {
            return Message.success("流程列表成功",workflowLinkMapper.getWorkFlowNodeList());
        } catch (Exception e) {
            log.error("流程列表异常信息:{}", e);
            return Message.error("流程列表失败");
        }
    }


    // 数据修改
    @GetMapping("/updateDBdata/v1")
    @ApiOperation(value = "数据修改", notes = "数据修改")
    public void updateDBdata() throws Exception {
        workflowExecService.updateDBdata();
    }

    // 数据修改
    @GetMapping("/updateDBdata1/v1")
    @ApiOperation(value = "数据修改1", notes = "数据修改1")
    public void updateDBdata1() {
        workflowExecService.updateDBdata1();
    }

    // 数据修改
    @GetMapping("/updateDBdata2/v1")
    @ApiOperation(value = "数据修改2", notes = "数据修改2")
    public void updateDBdata2() throws Exception {
        workflowExecService.updateDBdata2();
    }



    public static void main(String[] args) throws Exception {
//        for (int i = 0; i < 10; i++) {
//            System.err.println(IdGen.uuid());
//        }
//
//        String a = "1";
//        String substring = String.valueOf(a)
//                .substring(String.valueOf(a).lastIndexOf(",") + 1);
//        System.err.println(substring);



    }

}
